package com.loong.web.gp4j.view.users;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.loong.web.gp4j.bean.param.users.UserParam;
import com.loong.web.gp4j.bean.param.users.UserStatusParam;
import com.loong.web.gp4j.bean.param.users.UsersParam;
import com.loong.web.gp4j.bean.schema.code.Codes;
import com.loong.web.gp4j.bean.schema.user.Auths;
import com.loong.web.gp4j.bean.vo.user.UserVo;
import com.loong.web.gp4j.service.code.CodeService;
import com.loong.web.gp4j.service.user.UserService;
import com.loong.web.gp4j.view.util.UtilViewMapping;
import com.loong.web.suppert.collection.param.PageParam;
import com.loong.web.suppert.collection.vo.ResultVo;
import com.loong.web.suppert.collection.vo.ResultVoFactory;
import com.loong.web.suppert.intercept.auth.AuthType;

/**
 * 用户管理资源
 *
 * @author 张成轩
 */
@Controller
@RequestMapping("auth/users")
public class UsersViewMapping {

	@Resource
	private UtilViewMapping utilViewMapping;
	@Resource
	private UserService userService;
	@Resource
	private CodeService codeService;

	/**
	 * 用户管理页面
	 * 
	 * @param request 请求
	 * @param param 参数
	 * @param page 分页
	 * @return 视图
	 */
	@RequestMapping(value = "", method = RequestMethod.GET)
	@AuthType(Auths.USER_MANAGER)
	public ModelAndView users(HttpServletRequest request, UsersParam param, PageParam page) {

		ModelAndView model = new ModelAndView("gp4j/users/users");
		model.addObject("users", userService.queryUsers(request, param, page));
		model.addObject("statuses", userService.getStatuses());
		model.addObject("auths", userService.getAuths(request));
		model.addObject("order", userService.getOrder());
		return model;
	}

	/**
	 * 用户新增页面
	 * 
	 * @param request 请求
	 * @return 视图
	 */
	@RequestMapping(value = "edit", method = RequestMethod.GET)
	@AuthType(Auths.USER_MANAGER)
	public ModelAndView add(HttpServletRequest request) {

		ModelAndView model = new ModelAndView("gp4j/users/user-edit");
		model.addObject("auths", userService.getAuths(request));
		model.addObject("sexs", codeService.getCodeMap(Codes.SEX));
		return model;
	}

	/**
	 * 用户新增页面提交
	 * 
	 * @param request 请求
	 * @param id id
	 * @param param 参数
	 * @param valid 校验器
	 * @return 视图
	 */
	@RequestMapping(value = "edit", method = RequestMethod.POST, headers = "content-type=application/json")
	@ResponseBody
	@AuthType(Auths.USER_MANAGER)
	public ResultVo<?> addSubmit(HttpServletRequest request, @RequestBody @Valid UserParam param, BindingResult valid) {

		if (valid.hasErrors())
			return ResultVoFactory.fail(valid.getAllErrors().get(0).getDefaultMessage());
		return userService.add(request, param);
	}

	/**
	 * 用户编辑页面
	 * 
	 * @param request 请求
	 * @param id id
	 * @return 视图
	 */
	@RequestMapping(value = "edit/{id}", method = RequestMethod.GET)
	@AuthType(Auths.USER_MANAGER)
	public ModelAndView edit(HttpServletRequest request, @PathVariable int id) {

		UserVo user = userService.getUser(request, id);
		if (user == null)
			return utilViewMapping.error(request, "404");
		ModelAndView model = new ModelAndView("gp4j/users/user-edit");
		model.addObject("user", user);
		model.addObject("auths", userService.getAuths(request));
		model.addObject("sexs", codeService.getCodeMap(Codes.SEX));
		return model;
	}

	/**
	 * 用户编辑页面提交
	 * 
	 * @param request 请求
	 * @param id id
	 * @param param 参数
	 * @param valid 校验器
	 * @return 视图
	 */
	@RequestMapping(value = "edit/{id}", method = RequestMethod.POST, headers = "content-type=application/json")
	@ResponseBody
	@AuthType(Auths.USER_MANAGER)
	public ResultVo<?> editSubmit(HttpServletRequest request, @PathVariable Integer id,
			@RequestBody @Valid UserParam param, BindingResult valid) {

		if (valid.hasErrors())
			return ResultVoFactory.fail(valid.getAllErrors().get(0).getDefaultMessage());
		param.setId(id);
		return userService.edit(request, param);
	}

	/**
	 * 用户状态变更
	 * 
	 * @param request 请求
	 * @param param 参数
	 * @param valid 校验器
	 * @return 返回对象
	 */
	@RequestMapping(value = "status", method = RequestMethod.POST)
	@ResponseBody
	@AuthType(Auths.USER_MANAGER)
	public ResultVo<?> status(HttpServletRequest request, @Valid UserStatusParam param, BindingResult valid) {

		if (valid.hasErrors())
			return ResultVoFactory.fail(valid.getAllErrors().get(0).getDefaultMessage());
		return userService.editStatus(request, param);
	}

	/**
	 * 重置用户密码
	 * 
	 * @param request 请求
	 * @param id 用户id
	 * @return 返回对象
	 */
	@RequestMapping(value = "repwd", method = RequestMethod.POST)
	@ResponseBody
	@AuthType(Auths.USER_MANAGER)
	public ResultVo<?> repwd(HttpServletRequest request, Integer id) {

		if (id == null)
			return ResultVoFactory.fail("用户id不能为空");
		return userService.rePassword(request, id);
	}
}
